package it.getta.vicinometro.popup;

import it.getta.vicinometro.ActivityCallBack;
import it.getta.vicinometro.R;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;

public abstract class VicinometroPopup extends PopupWindow {

	protected Context mContext;

	private LayoutInflater mInflater;
	protected Handler mHandler = new Handler();
	protected ActivityCallBack mActivityCallBack;
	protected View mPopupRootView;

	public VicinometroPopup(Context context, ActivityCallBack activityCallBack, int contentViewId) {
		super(context);
		mContext = context;
		mActivityCallBack = activityCallBack;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		mPopupRootView = mInflater.inflate(contentViewId, null);
		mPopupRootView.setLayoutParams(new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		setContentView(mPopupRootView);
		setWidth(WindowManager.LayoutParams.FILL_PARENT);
		setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
		setBackgroundDrawable(new BitmapDrawable());
		setTouchable(true);
		setFocusable(true);
		setOutsideTouchable(true);
		setAnimationStyle(R.style.Animations_PopDownMenu_Reflect);
		setTouchInterceptor(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// Log.d("TAG", "onTouch");
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					dismiss();
					return true;
				}
				return false;
			}

		});

		
//		start(mPopupRootView);

	}
	
	public void setActivityCallBack(ActivityCallBack activityCallBack) {
		mActivityCallBack = activityCallBack;
	}

	public void show(View anchor) {
		showAtLocation(anchor, Gravity.CENTER, 0, 0);
	}
	
	public void show() {
		if (mPopupRootView != null) {
			show(mPopupRootView);
//			showAtLocation(mPopupRootView, Gravity.CENTER, 0, 0);
		}
	}
	
	public void setHandler(Handler handler) {
		mHandler = handler;
	}

	public void showPopup() {
		mActivityCallBack.popupShown(this);
	}

}
